package com.hech.hmall.controller;

import com.hech.hmall.annotation.TokenRequired;
import com.hech.hmall.constant.HttpConstants;
import com.hech.hmall.entity.ResponseEntity;
import com.hech.hmall.entity.UserEntity;
import com.hech.hmall.exception.UserException;
import com.hech.hmall.service.IUserService;
import com.hech.hmall.util.SystemUtil;
import com.hech.hmall.util.TokenHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author hanjiafeng
 * @date 2021/5/6 16:48
 * @desc todo
 */
@RestController
@RequestMapping("api/user")
public class UserController {
    private final IUserService userService;
    private final TokenHelper tokenHelper;

    public UserController(IUserService userService, TokenHelper tokenHelper) {
        this.userService = userService;
        this.tokenHelper = tokenHelper;
    }

    @PostMapping("register")
    public ResponseEntity<Void> register(UserEntity userEntity) {
        ResponseEntity<Void> responseEntity;
        if (checkAccount(userEntity.getAccount()).getData() ||
                checkPhone(userEntity.getPhone()).getData()) {
            return ResponseEntity.failure("账号或者手机号已存在");
        }
        int count = userService.register(userEntity);
        if (count == 1) {
            responseEntity = ResponseEntity.success("注册成功");
        } else {
            responseEntity = ResponseEntity.failure("注册失败,系统异常");
        }
        return responseEntity;
    }

    @GetMapping("checkAccount")
    public ResponseEntity<Boolean> checkAccount(String account) {
        ResponseEntity<Boolean> responseEntity;
        boolean isRepeat = userService.checkElement("account", account);
        responseEntity = ResponseEntity.success("查询成功");
        responseEntity.setData(isRepeat);
        return responseEntity;
    }

    @GetMapping("checkPhone")
    public ResponseEntity<Boolean> checkPhone(String phone) {
        ResponseEntity<Boolean> responseEntity;
        boolean isRepeat = userService.checkElement("phone", phone);
        responseEntity = ResponseEntity.success("查询成功");
        responseEntity.setData(isRepeat);
        return responseEntity;
    }

    @PostMapping("login")
    public ResponseEntity<Void> login(UserEntity userEntity, HttpServletResponse httpServletResponse) {
        ResponseEntity<Void> responseEntity;
        try {
            userEntity = userService.login(userEntity.getAccount(), userEntity.getPassword());
            SystemUtil.setLoginUser(userEntity);
            //生成token并保存到redis
            String accessToken = tokenHelper.createToken(userEntity);
            //将token返回客户端
            httpServletResponse.setHeader(HttpConstants.ACCESS_TOKEN, accessToken);
            responseEntity = ResponseEntity.success("登陆成功");
        } catch (UserException e) {
            responseEntity = ResponseEntity.failure("登陆失败:" + e.getMessage());
        }
        return responseEntity;
    }

    @TokenRequired
    @GetMapping("getUserInfo")
    public ResponseEntity<UserEntity> getUserInfo() {
        ResponseEntity<UserEntity> responseEntity = ResponseEntity.success("获取成功");
        responseEntity.setData(SystemUtil.getLoginUser());
        return responseEntity;
    }
}
